﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data.DBOES;
using Core;
using System.Data.SqlClient;
using System.Data;

namespace Data.Controllers
{
    public class NhatKyBanHangController:Controller<NhatKyBanHang>
    {
        //Find Comment width StartDay and EndDay of News
        public static List<NhatKyBanHang> FindStartEndDay(string spName, DateTime? startDay, DateTime? endDay, int? page, int? pageSize, ref int total)
        {
            Exception ex = new Exception();
            SqlParameter[] sqlParam = new SqlParameter[7];
            sqlParam[0] = new System.Data.SqlClient.SqlParameter("@Page", page);
            sqlParam[1] = new System.Data.SqlClient.SqlParameter("@PageSize", pageSize);
            sqlParam[2] = new System.Data.SqlClient.SqlParameter("@StartDate", startDay);
            sqlParam[3] = new System.Data.SqlClient.SqlParameter("@EndDate", endDay);
            sqlParam[4] = new System.Data.SqlClient.SqlParameter("@SortBy", null);
            sqlParam[5] = new System.Data.SqlClient.SqlParameter("@SortType", null);
            sqlParam[6] = new SqlParameter("@returnValue", 0);
            sqlParam[6].Direction = ParameterDirection.ReturnValue;
            System.Data.DataTable dt = ExecuteDataTable(spName, System.Data.CommandType.StoredProcedure, sqlParam, ref ex);

            List<NhatKyBanHang> list = new List<NhatKyBanHang>();

            if (dt == null)
                return list;

            foreach (System.Data.DataRow row in dt.Rows)
            {
                NhatKyBanHang item = new NhatKyBanHang();
                foreach (System.Data.DataColumn column in dt.Columns)
                {
                    foreach (System.Reflection.PropertyInfo property in item.GetType().GetProperties())
                    {
                        if (property.Name.ToLower() == column.ColumnName.ToLower())
                        {
                            if (row[column] != DBNull.Value)
                            {
                                property.SetValue(item, row[column], null);
                            }
                            break;
                        }
                    }
                }

                list.Add(item);
            }
            total = list.Count;
            return list;
        }
        //Find Comment width StartDay and EndDay of News
        public static List<NhatKyBanHang> NhatKyBanHang_Select_Distinct(string spName,string ColumnName)
        {
            Exception ex = new Exception();

            System.Data.SqlClient.SqlParameter columnName = new System.Data.SqlClient.SqlParameter("@fielName", ColumnName);
            System.Data.DataTable dt = ExecuteDataTable(spName, System.Data.CommandType.StoredProcedure, new System.Data.SqlClient.SqlParameter[] { columnName }, ref ex);
   
	
            List<NhatKyBanHang> list = new List<NhatKyBanHang>();

            if (dt == null)
                return list;

            foreach (System.Data.DataRow row in dt.Rows)
            {
                NhatKyBanHang item = new NhatKyBanHang();
                foreach (System.Data.DataColumn column in dt.Columns)
                {
                    foreach (System.Reflection.PropertyInfo property in item.GetType().GetProperties())
                    {
                        if (property.Name.ToLower() == column.ColumnName.ToLower())
                        {
                            if (row[column] != DBNull.Value)
                            {
                                property.SetValue(item, row[column], null);
                            }
                            break;
                        }
                    }
                }

                list.Add(item);
            }
            return list;
        }
    }
}
